<template>
  <div class="container">
    <div class="header">
      <div>
        <span class="title">商品名称：</span>
        <el-input v-model="search.name" placeholder="请输入内容" size="medium" />
      </div>

      <div>
        <span class="title">商品分类：</span>
        <el-select v-model="search.cat_id" placeholder="请选择" size="medium" style="width:100%">
          <el-option
            v-for="item in classifyData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>

      <div>
        <span class="title">创建时间：</span>
        <el-date-picker
          v-model="timer"
          size="medium"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
          :picker-options="pickerOptions"
          @change="selectTime"
        />
      </div>

      <div>
        <el-button size="small" type="primary" @click="$router.push('/goods/edit')">添加</el-button>
        <el-button size="small" type="primary" @click="reset">重置</el-button>
        <el-button size="small" type="primary" @click="getList">搜索</el-button>
      </div>
    </div>

    <div class="content">
      <el-table
        v-loading="loading"
        :data="goodsData"
        :header-cell-style="{'color':'#000'}"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="100"
          align="center"
        />
        <el-table-column
          prop="name"
          label="商品名称"
          align="center"
          width="300"
        />
        <el-table-column
          prop="name"
          label="商品图片"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <img :src="scope.row.image" alt="" style="width:78px;height:78px;object-fit: cover;border-radius: 10px;">
          </template>
        </el-table-column>
        <el-table-column
          prop="selling_price"
          label="商品价格"
          align="center"
          width="180"
        />
        <el-table-column
          prop="stock"
          label="商品库存"
          align="center"
          width="180"
        />
        <el-table-column
          prop="sale"
          label="商品销量"
          align="center"
          width="180"
        />
        <el-table-column
          label="更新时间"
          align="center"
          width="300"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.updated_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <div class="btn">
              <span @click="changeStatus(scope.$index, scope.row)">{{ scope.row.status == 1 ? '下架' : '上架' }}</span>
              <span @click="handleEdit(scope.row)">编辑</span>
              <span @click="handleDelete(scope.row)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="pagesize"
          :page-sizes="[6, 10, 20, 30]"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      timer: '',
      search: {
        cat_id: '',
        name: '',
        start_time: '',
        end_time: ''
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },

      classifyData: [], // 分类
      goodsData: [],
      total: 0,
      pagesize: 6, // 默认一页6条
      currentPage: 1 // 当前页
    }
  },
  created() {
    this.getClassifyData()
    this.getList()
  },
  methods: {
    // 重置
    reset() {
      this.currentPage = 1
      this.pagesize = 6
      this.search.cat_id = ''
      this.search.name = ''
      this.search.start_time = ''
      this.search.end_time = ''
      this.timer = ''
      this.goodsData = []
      this.getList()
    },

    // 获取分类数据
    getClassifyData() {
      this.$request.get('/admin/goods_category').then(res => {
        this.classifyData = res.data.data
      }).catch()
    },

    // 获取表格数据
    getList() {
      this.loading = true
      this.$request.get('/admin/goods', {
        params: {
          ...this.search,
          limit: this.pagesize,
          page: this.currentPage
        }
      }).then(res => {
        this.loading = false
        this.goodsData = res.data.data
        this.total = res.data.total
      }).catch()
    },

    // 设置每页条数
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 选择时间
    selectTime(e) {
      this.search.start_time = e[0]
      this.search.end_time = e[1]
    },

    // 修改状态
    changeStatus(idx, e) {
      this.$request.post(`/admin/goods/status/${e.id}`, { status: e.status == 1 ? 0 : 1 }).then(res => {
        this.goodsData[idx].status = e.status == 1 ? 0 : 1
      }).catch()
    },

    // 编辑
    handleEdit(row) {
      this.$router.push({ path: '/goods/edit', query: { id: row.id }})
    },

    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$request.post(`/admin/goods/delete/${row.id}`).then(_ => {
          this.getList()
          this.$message({ message: '删除成功', type: 'success' })
        }).catch()
      }).catch()
    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    width: 100%;
    height: auto;
    padding: 10px;
    background-color: #fff;
    display: flex;
    justify-content: space-between;

    &>div{
      width: calc(100% / 4 - 20px);
      display: flex;
      align-items: center;

      &:nth-child(3){
        width: calc(100% / 4 + 60px);
      }
      &:nth-child(4){
        width: calc(100% / 4 - 100px);
      }
      &>.title{
        display: inline-block;
        width: 100px;
        font-size: 14px;
      }
    }
  }
  .content{
    width: 100%;
    margin-top: 10px;
  }
  .pagination{
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .btn{
    &>span{
      display: inline-block;
      color: #1989fa;
      width: 40px;
      text-align: center;
      cursor: pointer;
    }
  }
</style>
